package com.work.keep.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.work.keep.R;
import com.work.keep.datas.WorkoutData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/16.
 */
public class MyWorkoutBaseAdapter extends BaseAdapter {
    private Context context;
    private List<WorkoutData.DataEntity.ResultsEntity.PlansEntity> workoutPlansLists;

    public MyWorkoutBaseAdapter(Context context) {
        this.context = context;
        workoutPlansLists = new ArrayList<>();
    }

    public void addWorkoutDataLists(List<WorkoutData.DataEntity.ResultsEntity.PlansEntity> workoutPlansLists) {
        this.workoutPlansLists = workoutPlansLists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return workoutPlansLists.size();
    }

    @Override
    public Object getItem(int position) {
        return workoutPlansLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyWorkoutViewHolder myWorkoutViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_playground_workout_item, parent, false);
            myWorkoutViewHolder = new MyWorkoutViewHolder(convertView);
            convertView.setTag(myWorkoutViewHolder);
        } else {
            myWorkoutViewHolder = (MyWorkoutViewHolder) convertView.getTag();
        }
        myWorkoutViewHolder.pioneer.setText(workoutPlansLists.get(position).getPioneer() + "");
        myWorkoutViewHolder.planName.setText(workoutPlansLists.get(position).getName());
        String transpoint = "";
        for (int i = 0; i < workoutPlansLists.get(position).getTrainingpoints().size(); i++) {
            transpoint += workoutPlansLists.get(position).getTrainingpoints().get(i).getName();
        }
        myWorkoutViewHolder.daysAndTranspoint.setText(workoutPlansLists.get(position).getDays() + " " + transpoint);
        myWorkoutViewHolder.planLevel.setText("K" + workoutPlansLists.get(position).getDifficulty());
        String equitments = "";
        for (int i = 0; i < workoutPlansLists.get(position).getEquipments().size(); i++) {
            equitments += " " + workoutPlansLists.get(position).getEquipments().get(i);
        }
        myWorkoutViewHolder.planEquipment.setText(equitments);
        return convertView;
    }

    class MyWorkoutViewHolder {
        private TextView planName, planLevel, planEquipment, daysAndTranspoint, pioneer;

        public MyWorkoutViewHolder(View itemView) {
            planName = (TextView) itemView.findViewById(R.id.planName);
            planLevel = (TextView) itemView.findViewById(R.id.planLevel);
            planEquipment = (TextView) itemView.findViewById(R.id.planEquipment);
            daysAndTranspoint = (TextView) itemView.findViewById(R.id.daysAndTranspoint);
            pioneer = (TextView) itemView.findViewById(R.id.pioneer);
        }
    }
}
